<?php defined('SYSPATH') or die('No direct script access.');

class Model_Review_Rozetka extends Model_Review_Abstract
{ 
    protected $_table_name = 'rozetka_reviews';  
    
    public function get_entity()
    {
        return 'rozetka';
    }
    
    public function get_reviews_query($entity)
    {
        // Если ищем по товару или модели
        if ($entity instanceof Model_Product) {
            $model_id = $entity->model_id; 
        } else if ($entity instanceof Model_Model) {
            $model_id = $entity->id;
        } else {
            throw new Model_Exception('Некорректный тип');
        }
        return DB::select(
                array(DB::expr('\'rozetka\''), 'entity'),
                'id',
                'model_id',
                'source_id',
                'user_nick',
                array(DB::expr('NULL'), 'user_link'),
                'rating',
                array(DB::expr('NULL'), 'experience'),
                'pluses',
                'minuses',
                'text',
                'link',
                'date',
                'usefulness',
                'u_yes',
                'u_no'
            )
            ->from($this->_table_name)
            ->where('model_id', '=', $model_id)
            ->and_where('visible', '=', 1);    
    }
    
    public function add_review(Model_Model $model, $source_id, $text, $pluses, $minuses, $rating,
        $user_nick, $link, $date, $u_yes, $u_no, $visible)
    {
        $this->model_id   = $model->id;
        $this->source_id  = $source_id;
        $this->text       = $text;
        $this->pluses     = $pluses;
        $this->minuses    = $minuses;
        $this->rating     = $rating;
        $this->user_nick  = $user_nick;
        $this->link       = $link;
        $this->date       = $date;
        $this->u_yes      = $u_yes;
        $this->u_no       = $u_no;
        $this->usefulness = $this->get_usefulness();
        $this->visible    = $visible;
        $this->save();
        
        if ($visible) {
            $model->change_count_reviews($this->get_entity());
            if ($rating) {
                $model->change_count_stars($rating, $this->get_entity());
            }
        }
        
        return $this;  
    }    
}